package com.dataseek.iot.device.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.device.dto.LiquidJarDto;
import com.dataseek.iot.device.service.AlarmLogService;
import com.dataseek.iot.device.service.DeviceListService;
import com.dataseek.iot.device.service.LiquidJarService;
import com.dataseek.iot.device.vo.LiquidJarShowVo;
import com.dataseek.iot.device.vo.LiquidJarVo;
import com.dataseek.iot.device.vo.NotifyLogVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.response.ResponseResult;
import com.dataseek.iot.toolkit.mybatis.entity.NotifyLog;
import com.dataseek.iot.toolkit.mybatis.mapper.NotifyLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 首页和全局搜索
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("**/index")
public class IndexController {
    @Autowired
    private LiquidJarService liquidJarService;

    @Autowired
    private DeviceListService deviceListService;

    @Autowired
    private AlarmLogService alarmLogService;

    @Autowired
    private NotifyLogMapper notifyLogeMapper;

    @RequestMapping(value = "/searchIndex", method = RequestMethod.GET)
    public ResponseResult searchIndex(@RequestParam(value="page")  Integer page,
                                      @RequestParam(value="size")  Integer size,
                                      @RequestParam(value="showType", required=true) String showType,
                                      @RequestParam(value="searchText", required=false) String searchText
                                      ) throws IOException {
        //根据showType查询不同的分页
        if(showType.equals("1")) {//1查询液位分页情况
            LiquidJarVo tenantVo = liquidJarService.queryAction(searchText, null, page, size);
            return new ResponseResult(RspStatusEnum.SUCCESS.getCode(), "成功", tenantVo);
        }else if(showType.equals("2")){//2表示查询储液罐和设备信息
            LiquidJarDto qryDto=new LiquidJarDto();
            LiquidJarShowVo liquidJarShowVo =new LiquidJarShowVo();

            qryDto.setFactoryName(searchText);//设置厂区信息
            liquidJarShowVo = liquidJarService.queryJarShowIndex(qryDto,page,size);

            return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),liquidJarShowVo);
        }else{//返回告警日志信息
            NotifyLogVo logVo = new NotifyLogVo();
            QueryWrapper<NotifyLog> qw = new QueryWrapper<>();
            qw.like("device_code",searchText)
              .or()
              .like("device_name",searchText)
              .or()
              .like("notify_msg",searchText);
            qw.orderBy(true, false, "create_time");
            IPage<NotifyLog> notifyLogIPage = new Page<>(page, size);
            List<NotifyLog> mapList = notifyLogeMapper.selectPage(notifyLogIPage, qw).getRecords();
            logVo.setPage(page);
            logVo.setSize(size);
            logVo.setTotal(notifyLogIPage.getTotal());
            logVo.setList(mapList);
            return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),logVo);
        }
    }


}
